package toolTips
{
	import caurina.transitions.Tweener;
	
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.text.Font;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.utils.Timer;
	
	import mx.logging.ILogger;
	import mx.logging.Log;
	import mx.logging.LogEventLevel;
	
	public class ToolTipCreator extends MovieClip
	{
		
		private var _adjustToText:Boolean;
		
		private var _fillColor:uint = 0x666666;
		private var _lineColor:uint = 0xFFFFFF;
		
		private var _fillAlpha:Number = 1;
		private var _lineAlpha:Number = 1;
		
		private var _boxWidth:Number;
		private var _boxHeight:Number;
		
		private var _globeWidth:Number = 10;
		private var _globeHeight:Number = 3;
		
		private var _roundCorner:Number = 8;
		
		private var _lineOffSet:Number = 10;
		private var _lineThikness:Number = 2;
		
		private var _textFontType:String = "";
		private var _textHeight:Number = 12;
		private var _textFont:String = "Arial";
		private var _textCoulor:uint = 0xFFFFFF;
		private var _inputText:String;
		
		private var _transitionTime:Number=0.2;
		private var _showTime:Number=3000;
		private var _waitTime:Number=2000;
		private var _actionTimer:Timer;
		private var _showTimer:Timer;
		
		private var _emiterWidth:Number;
		private var _emiterHeight:Number;
		
		private var _position:String="top";
		
		private var _toolTipEmiter:MovieClip;
		private var _container:MovieClip;
		private var _bodyShape:Shape;
		private var _triangleShape:Shape;
		private var _newText:TextField;
		private var _format:TextFormat;
		
		private var _tipEnabled:Boolean;
		
		private static var logger:ILogger = Log.getLogger("toolTipCreator");

		
		public function ToolTipCreator(toolTipEmiter:MovieClip, inputText:String, fillColor:uint, lineColor:uint, adjustToText:Boolean=true, boxWidth:Number=200, boxHeight:Number=50)
		{	
			logger.log(LogEventLevel.INFO, "toolTipCreator");
			_toolTipEmiter=toolTipEmiter;
			_inputText=inputText;
			_fillColor=fillColor;
			_lineColor=lineColor;
			_adjustToText=adjustToText;
			_boxWidth=boxWidth;
			_boxHeight=boxHeight;
			
			_emiterWidth=_toolTipEmiter.width;
			_emiterHeight=_toolTipEmiter.height;
			
			_container=new MovieClip;

			setupCounter();
			createToolTip();

		}
		
		private function createToolTip():void{
			while (_container.numChildren>0){
				_container.removeChildAt(0);
			}
			_bodyShape=new Shape;
			_newText=new TextField;
			_format=new TextFormat;
			_container.visible=false;
			_container.alpha=0;
			
			_lineOffSet = _globeWidth / 2;
			_newText.x = _globeWidth;
			
			_format.size = _textHeight;
			_format.color = _textCoulor;
			
			if(Font.enumerateFonts().length>0) {
				textFont=Font.enumerateFonts(false)[0].fontName;
			} else {
				textFont=Font.enumerateFonts(true)[0].fontName;
			}

			_newText.defaultTextFormat = _format;
			_newText.autoSize = "left";
			_newText.multiline = false;
			_newText.selectable = false;
			_newText.text = _inputText;
			
			if (_adjustToText){
				_boxWidth=_newText.textWidth+_roundCorner/2;
				_boxHeight=_newText.textHeight;
				_newText.x = _roundCorner;
				_newText.y = -_globeHeight-_roundCorner-_roundCorner-_boxHeight/2;
				createBox();
			}else{
				_newText.x = _roundCorner;
				_newText.y = -_globeHeight-_roundCorner-_roundCorner-_boxHeight/2;
				createBox();
			}
			
			_container.addChild(_bodyShape);
			_container.addChild(_newText);
			setContainerPosition();
			
			tipEnabled=true;
		}
		
		private function setContainerPosition():void {
			
			switch(_position) {
				case "top":
					_container.x = _emiterWidth/2;
					_container.y = -5;
					break;
				case "bottom":
					
					break;
				case "left":
					
					break;
				case "right":
					
					break;
				default:
					_container.x = _emiterWidth/2;
					_container.y = _toolTipEmiter.y-_toolTipEmiter.height-5;
					break;
			}
			
		}
		
		public function showToolTip():void {
			_toolTipEmiter.addChild(_container);
			_container.visible=true;
			Tweener.addTween(_container, {alpha:1, time:_transitionTime, transition:"linear", onComplete:hideToolTip});
		
		}
		
		public function hideToolTip():void {
			_showTimer= new Timer(_showTime, 1);
			_showTimer.start();
			_showTimer.addEventListener(TimerEvent.TIMER_COMPLETE, hide);	
		}
		
		private	function hide(event:TimerEvent):void {
			_showTimer.reset();
			fadeOut();
		}	
		
		private function fadeOut():void {
			Tweener.addTween(_container, {alpha:0, time:_transitionTime, transition:"linear", onComplete:removeToolTip});
		}
		
		private function removeToolTip():void {
			_container.visible=false;
		}
		
		private function setupCounter():void {
			_actionTimer = new Timer(_waitTime, 1);
			_actionTimer.addEventListener(TimerEvent.TIMER_COMPLETE, activateShow);
		}
		
		private function activateShow(event:TimerEvent):void {
			_actionTimer.reset();
			showToolTip();
		}
		
		private function initCounter(event:MouseEvent):void {
			_actionTimer.start();
		}
		
		private function stopCounter(event:MouseEvent):void {
			_actionTimer.stop();
			_actionTimer.reset();
			fadeOut();
		}
		
		private function createBox():void{
			
			_bodyShape.graphics.clear();
			_bodyShape.graphics.lineStyle(_lineThikness, _lineColor, _lineAlpha);
			_bodyShape.graphics.beginFill(_fillColor, _fillAlpha);
			_bodyShape.graphics.moveTo(0,0);
			_bodyShape.graphics.lineTo(0,-_globeHeight-_boxHeight-_roundCorner);
			_bodyShape.graphics.curveTo(0,-_globeHeight-_boxHeight-_roundCorner-_roundCorner, _roundCorner, -_globeHeight-_boxHeight-_roundCorner-_roundCorner);
			_bodyShape.graphics.lineTo(_roundCorner+_boxWidth, -_globeHeight-_boxHeight-_roundCorner-_roundCorner);
			_bodyShape.graphics.curveTo(_roundCorner+_boxWidth+_roundCorner,-_globeHeight-_boxHeight-_roundCorner-_roundCorner, _roundCorner+_boxWidth+_roundCorner, -_globeHeight-_boxHeight-_roundCorner);
			_bodyShape.graphics.lineTo(_roundCorner+_boxWidth+_roundCorner, -_globeHeight-_roundCorner);
			_bodyShape.graphics.curveTo(_roundCorner+_boxWidth+_roundCorner, -_globeHeight, _roundCorner+_boxWidth, -_globeHeight);
			_bodyShape.graphics.lineTo(_globeWidth, -_globeHeight);
			_bodyShape.graphics.endFill();
		}

		public function get adjustToText():Boolean
		{
			return _adjustToText;
		}

		public function set adjustToText(value:Boolean):void
		{
			_adjustToText = value;
			createToolTip();
		}

		public function get fillColor():uint
		{
			return _fillColor;
		}

		public function set fillColor(value:uint):void
		{
			_fillColor = value;
			createToolTip();
		}

		public function get lineColor():uint
		{
			return _lineColor;
		}

		public function set lineColor(value:uint):void
		{
			_lineColor = value;
			createToolTip();
		}

		public function get fillAlpha():Number
		{
			return _fillAlpha;
		}

		public function set fillAlpha(value:Number):void
		{
			_fillAlpha = value;
			createToolTip();
		}

		public function get lineAlpha():Number
		{
			return _lineAlpha;
		}

		public function set lineAlpha(value:Number):void
		{
			_lineAlpha = value;
			createToolTip();
		}

		public function get boxWidth():Number
		{
			return _boxWidth;
		}

		public function set boxWidth(value:Number):void
		{
			_boxWidth = value;
			createToolTip();
		}

		public function get boxHeight():Number
		{
			return _boxHeight;
		}

		public function set boxHeight(value:Number):void
		{
			_boxHeight = value;
			createToolTip();
		}

		public function get globeWidth():Number
		{
			return _globeWidth;
		}

		public function set globeWidth(value:Number):void
		{
			_globeWidth = value;
			createToolTip();
		}

		public function get globeHeight():Number
		{
			return _globeHeight;
		}

		public function set globeHeight(value:Number):void
		{
			_globeHeight = value;
			createToolTip();
		}

		public function get lineOffSet():Number
		{
			return _lineOffSet;
		}

		public function set lineOffSet(value:Number):void
		{
			_lineOffSet = value;
			createToolTip();
		}

		public function get lineThikness():Number
		{
			return _lineThikness;
		}

		public function set lineThikness(value:Number):void
		{
			_lineThikness = value;
			createToolTip();
		}

		public function get textFontType():String
		{
			return _textFontType;
		}

		public function set textFontType(value:String):void
		{
			_textFontType = value;
			createToolTip();
		}

		public function get textHeight():Number
		{
			return _textHeight;
		}

		public function set textHeight(value:Number):void
		{
			_textHeight = value;
			createToolTip();
		}

		public function get textCoulor():uint
		{
			return _textCoulor;
		}

		public function set textCoulor(value:uint):void
		{
			_textCoulor = value;
			createToolTip();
		}

		public function get inputText():String
		{
			return _inputText;
		}

		public function set inputText(value:String):void
		{
			_inputText = value;
			createToolTip();
		}	

		public function get showTime():Number
		{
			return _showTime;
		}

		public function set showTime(value:Number):void
		{
			_showTime = value;			
			createToolTip();
		}

		public function get waitTime():Number
		{
			return _waitTime;
		}

		public function set waitTime(value:Number):void
		{
			_waitTime = value;
		}

		public function get transitionTime():Number
		{
			return _transitionTime;
		}

		public function set transitionTime(value:Number):void
		{
			_transitionTime = value;
		}

		public function get position():String
		{
			return _position;
		}

		public function set position(value:String):void
		{
			_position = value;
			createToolTip();
		}

		public function get tipEnabled():Boolean
		{
			return _tipEnabled;
		}

		public function set tipEnabled(value:Boolean):void
		{
			_tipEnabled = value;

			if(_tipEnabled) {
				_toolTipEmiter.addEventListener(MouseEvent.MOUSE_OVER, initCounter);
				_toolTipEmiter.addEventListener(MouseEvent.MOUSE_OUT, stopCounter);
			}else{
				_toolTipEmiter.removeEventListener(MouseEvent.MOUSE_OVER, initCounter);
				_toolTipEmiter.removeEventListener(MouseEvent.MOUSE_OUT, stopCounter);

			}

		}

		public function get newText():TextField
		{
			return _newText;
		}

		public function set newText(value:TextField):void
		{
			_newText = value;
		}

		public function get roundCorner():Number
		{
			return _roundCorner;
		}

		public function set roundCorner(value:Number):void
		{
			_roundCorner = value;
		}

		public function get textFont():String
		{
			return _textFont;
		}

		public function set textFont(value:String):void
		{
			_textFont = value;
			_format.font=_textFont;
			_newText.embedFonts=true;
		}


	}
}